package com.lanan.springboot.util;

import org.springframework.util.StringUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @desc StringUtil
 * @author lanan
 * @date 2022-04-30 21:56:30
 **/
public class StringUtil {

    private StringUtil() {
    }

    /**
     * @desc 手机号正则
     **/
    public static final String MOBILEREGEX = "^1[0-9]{10}";
    /**
     * @desc email正则
     **/
    public static final String EMAILREGEX = "^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
    /**
     * @desc url正则
     **/
    public static final String URLREGEX = "^((https|http)?:\\/\\/)[^\\s]+";
    /**
     * @desc 用户名正则
     **/
    public static final String USERNAMEREGEX = "^(?!_)(?!.*?_$)[a-zA-Z0-9_\\u4e00-\\u9fa5]{4,10}$";
    /**
     * @desc 密码正则
     **/
    public static final String PASSWORDREGEX = "^(?![A-z0-9]+$)(?![A-z~@*()_]+$)(?![0-9~@*()_]+$)([A-z0-9~@*()_]{8,})$";
    /**
     * @desc 生成随机字符串用到的字符数组
     **/
    private static final char[] HEX_DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',
            'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
            'z'};
    /**
     * @desc 生成随机长度的数字用到的数组
     **/
    private static final char[] DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    public static final Random RANDOM = new Random();

    public static boolean check(String text, String regex) {
        if (StringUtils.isEmpty(text)) {
            return false;
        } else {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(text);
            return matcher.matches();
        }
    }

    /**
     * @desc 随机指定长度的字符串
     * @author lanan
     * @date 2022-04-09 21:34:44
     * @param length
     * @return java.lang.String
     **/
    public static String randomString(int length) {
        StringBuilder sb = new StringBuilder();
        for (int loop = 0; loop < length; ++loop) {
            sb.append(HEX_DIGITS[RANDOM.nextInt(HEX_DIGITS.length)]);
        }
        return sb.toString();
    }

    /**
     * @desc 随机指定长度的数字
     * @author lanan
     * @date 2022-04-09 21:34:53
     * @param length
     * @return java.lang.String
     **/
    public static String randomNumber(int length) {
        StringBuilder sb = new StringBuilder();
        for (int loop = 0; loop < length; ++loop) {
            sb.append(DIGITS[RANDOM.nextInt(DIGITS.length)]);
        }
        return sb.toString();
    }

    /**
     * @desc 生成一个uuid
     * @author lanan
     * @date 2022-04-09 21:35:03
     * @return java.lang.String
     **/
    public static String uuid() {
        return UUID.randomUUID().toString();
    }

    /**
     * @desc 检测是否是用户accessToken
     * @author lanan
     * @date 2022-04-09 21:35:18
     * @param accessToken
     * @return boolean
     **/
    public static boolean isUUID(String accessToken) {
        if (StringUtils.isEmpty(accessToken)) {
            return false;
        } else {
            try {
                // noinspection ResultOfMethodCallIgnored
                UUID.fromString(accessToken);
                return true;
            } catch (Exception e) {
                return false;
            }
        }
    }

    /**
     * @desc 格式化url参数部分返回map  params格式：a=1&b=2&c=3  返回：{a: 1, b: 2, c: 3}
     * @author lanan
     * @date 2022-04-09 21:36:43
     * @param params
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    public static Map<String, Object> formatParams(String params) {
        if (StringUtils.isEmpty(params)) {
            return null;
        }
        Map<String, Object> map = new HashMap<>(16);
        for (String s : params.split("&")) {
            String[] ss = s.split("=");
            map.put(ss[0], ss[1]);
        }
        return map;
    }

    /**
     * @desc 查找评论里at的用户名
     * @author lanan
     * @date 2022-04-09 21:36:15
     * @param content
     * @return java.util.List<java.lang.String>
     **/
    public static List<String> fetchAtUser(String content) {
        if (StringUtils.isEmpty(content)) {
            return Collections.emptyList();
        }
        // 去掉 ``` ``` 包围的内容
        content = content.replaceAll("```([\\s\\S]*)```", "");
        // 去掉 ` ` 包围的内容
        content = content.replaceAll("`([\\s\\S]*)`", "");
        // 找到@的用户
        String atRegex = "@[a-z0-9-_]+\\b?";
        List<String> atUsers = new ArrayList<>();
        Pattern regex = Pattern.compile(atRegex);
        Matcher regexMatcher = regex.matcher(content);
        while (regexMatcher.find()) {
            atUsers.add(regexMatcher.group());
        }
        return atUsers;
    }

    /**
     * @desc 去掉数组里的空值和重复数据
     * @author lanan
     * @date 2022-04-09 21:36:26
     * @param strs
     * @return java.util.Set<java.lang.String>
     **/
    public static Set<String> removeEmpty(String[] strs) {
        if (strs == null || strs.length == 0) {
            return Collections.emptySet();
        }
        Set<String> set = new HashSet<>();
        for (String str : strs) {
            if (!StringUtils.isEmpty(str)) {
                set.add(str);
            }
        }
        return set;
    }

}
